package com.xca.okhttp.views;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xca.okhttp.R;
import com.xca.okhttp.contract.IDictionaryContact;
import com.xca.okhttp.entity.Dictionaries;
import com.xca.okhttp.presenter.DictionaryPresenter;
import com.xca.okhttp.utils.LanguageUtils;

public class DictionaryFragment extends Fragment implements IDictionaryContact.DictionaryView , View.OnClickListener{
    private IDictionaryContact.DictionaryPresenter dPresenter ;
    private Dictionaries.DataDictionary dData ;
    private View view ;
    private EditText dInput ;
    private ImageButton dSea ;
    private TextView dWen , dPin , dBu , dBi , dZu , dJian , dXiang ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null){
            view = inflater.inflate(R.layout.dictionary , container , false);
            init();
        }
        return view ;
    }

    private void init(){
        dInput = view.findViewById(R.id.dInput);
        dSea = view.findViewById(R.id.dSea);
        dWen = view.findViewById(R.id.dWen);
        dPin = view.findViewById(R.id.dPin);
        dBu = view.findViewById(R.id.dBu);
        dBi = view.findViewById(R.id.dBi);
        dZu = view.findViewById(R.id.dZu);
        dJian = view.findViewById(R.id.dJian);
        dXiang = view.findViewById(R.id.dXiang);
        dPresenter = new DictionaryPresenter(this, getContext());
        dSea.setOnClickListener(this);
    }

    @Override
    public void onHttpLoading() {

    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void showData(Dictionaries data) {
        dData = data.getResult();
        if (dData != null){
            dWen.setText(dData.getHanzi());
            dPin.setText(dData.getPinyin());
            dBu.setText(dData.getBushou());
            dBi.setText(dData.getBihua());
            dZu.setText(dData.getWords());
            String[] jian = dData.getBasic_explain();
            dJian.setText(getExplain(jian));
            String[] xiang = dData.getDetail_explain();
            dXiang.setText(getExplain(xiang));
        }else {
            Toast.makeText(getActivity(), "数据不存在", Toast.LENGTH_SHORT).show();
        }
    }
    private String getExplain(String[] array){
        StringBuilder sb = new StringBuilder();
        for (String s : array){
            sb.append(s + "\n");
        }
        return sb.toString();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId() ;
        if (id == R.id.dSea){
            String input = dInput.getText().toString().trim();
            boolean isEnglish = LanguageUtils.isPureEnglish(input);
            if (!input.isEmpty()){
                if (!isEnglish){
                    dPresenter.getData(input);
                }else {
                    Toast.makeText(getActivity(), "请输入文字", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(getActivity(), "请输入需要查询的文字", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
